//LiuZh 2017-06-27
import React, { Component } from 'react';

import {
	View,
  Image,
  Dimensions,
  TouchableWithoutFeedback,
  StyleSheet,
  Text,
  FlatList
} from 'react-native';


let winWidth  = Dimensions.get('window').width;//屏幕宽度

import  InitDB  from 'shequnsiweiapp/src/core/database/InitDB';


let voicess = [];

export default class PurchasedScene extends Component{

	constructor(props){
		super(props);

		this.state = {
			voices: [],
		}
	}

	render() {

    
		return(
			<View style={styles.body}>
				{
					InitDB.getCourseUserCurrentUser() &&   InitDB.getCourseUserCurrentUser().length>0   &&  InitDB.getCourseUserCurrentUser()[0].ID_ ? 
						(<FlatList data={this.state.voices}
						style={{flex:1}}
          	renderItem={this.renderFLItem}/>) : 
						(<Text style={{fontSize: 22, color: '#000'}}>没有信息...</Text>)
					
				}
			</View>
	)};

	renderFLItem = (data) => {
		let title='';
		let curr;
		for(let i = 0; i < voicess.length; i++){
			if (data.item.COURSE_ID == voicess[i].COURSE_ID) {
				title = voicess[i].TITLE1;
				curr = i;
			}
		}
		return(
			<TouchableWithoutFeedback
				onPress={() => {this.pressItem(curr)}}>
			<View	
				style={{height: 65,width:winWidth, padding: 20,marginBottom:0.6,backgroundColor: '#FFF',flexDirection: 'row',justifyContent:'center',alignItems: 'center'}}>
				<Image style={{height: 40,width:40,marginRight: 16,}}
					source={require('shequnsiweiapp/src/img/self_zhaoliying.png')}/>
				<View style={{flex:1}}>
					<Text>{title}</Text>
					<View style={{flex:1}}/>
				</View>
			</View>
			</TouchableWithoutFeedback>
		)
	}

    componentDidMount () {

        let realmObj = InitDB.getVoices();
        for(let i = 0; i < realmObj.length; i++){
          voicess.push(realmObj[i]);
        }

        let realmObjs = InitDB.getCourseUser();
        let voices = this.state.voices;
        for(let i = 0; i < realmObjs.length; i++){
          voices.push(realmObjs[i]);
        }
        this.setState({
            voices: voices,
        });

    }


  pressItem = (curr) => {
  	// alert(voicess[curr])
  	// alert(voicess.length)
		this.props.screenProps.appNavigation.navigate('Video', {
			itemData: voicess[curr],
      voices: voicess,
		});
	}
}

const styles = StyleSheet.create({
	body: {
		backgroundColor: '#DDD',
		flex: 1,
		alignItems:'center',
		justifyContent:'center',
	},
});
